// pages/register/register.js
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        registeriphone: '',
        registernum: '',
        registerpassword: '',
        registerclass: '',
        registername: '',
        registerway: '',
        strloginName: '', //登录姓名
        strloginClass: '', //登录班级
        strloginWay: '', //登录组别
        strloginUser: '', //登录用户名
        strloginpassword: '', //用户密码
        switchChecked: '', //选中按钮
    },

    denglu: function () {
        wx.redirectTo({
            url: '/pages/login/login',
        })
    },
    //获取是否记住密码的状态值
    bindswitchchange: function (e) {
        console.log(e)
        var that = this
        var x = e.detail.value
        if (x == true) {
            that.setData({
                switchChecked: x,
            })
        } else if (x == false) {
            that.setData({
                switchChecked: x,
            })
        }
        console.log(that.data.switchChecked + '状态')
    },
    inputclass: function (e) {
        console.log(e.detail.value)
        var that = this
        var a = e.detail.value
        that.setData({
            registerclass: a
        })

    },
    inputname: function (e) {
        console.log(e.detail.value)
        var that = this
        var b = e.detail.value
        that.setData({
            registerclass: b
        })
    },
    inputway: function (e) {
        console.log(e.detail.value)
        var that = this
        var c = e.detail.value
        that.setData({
            registerclass: c
        })
    },
    //注册手机号判断
    inputnum: function (e) {
        console.log(e.detail.value)
        var that = this
        var x = e.detail.value
        if (x != '' && (x.length == 12)) {
            that.setData({
                registeriphone: x
            })
            if (that.data.registerpassword != '')
                that.setData({
                    loginbtnstate2: false
                })

            else {
                that.setData({
                    loginbtnstate2: true
                })
            }
        } else {
            that.setData({
                loginbtnstate2: true
            })
        }
    },
    //注册密码判断
    password2: function (e) {
        console.log(e.detail.value)
        var that = this
        var x = e.detail.value
        if (x != '') {
            that.setData({
                registerpassword: x
            })
            if (that.data.registeriphone != '' && (that.data.registeriphone.length == 12)) {
                that.setData({
                    loginbtnstate2: false
                })
            } else {
                that.setData({
                    loginbtnstate2: true
                })
            }
        }
    },

    //注册
    formsubmit: function (e) {
        console.log(e.detail.value)
        var obj = {}
        obj.stuNum = e.detail.value.registerinputnum
        obj.password = e.detail.value.registerinputpassword
        obj.name = e.detail.value.registerinputname
        obj.class = e.detail.value.registerinputclass
        obj.depart = e.detail.value.registerinputway
        obj.yqm = e.detail.value.yqm

        console.log(obj.name)

        wx.request({
            url: app.globalData.requestContext + '/register',
            method: "post",
            data: {
                userName: obj.name,
                account: obj.stuNum,
                stu_class: obj.class,
                depart: obj.depart,
                password: obj.password,
                yqm: obj.yqm

            },

            header: {
                'content-type': "application/x-www-form-urlencoded"
            },
            success: function (result) {
                var res = result.data
                if (res.code == 0) {
                    wx.showToast({
                        title: '注册成功',
                        icon: 'success',

                    })


                    setTimeout(function () {
                        wx.reLaunch({

                            url: '/pages/login/login'

                        })
                    },1500)
                } else if (res.code == 1) {
                    wx.showToast({
                        title: res.msg,
                        icon: 'error',
                        duration: 2000
                    })
                    return
                }


            }
        })
        //    wx.showToast({
        //      title: '注册成功',
        //    })
        //    wx.navigateTo({
        //     url: '/pages/login/login',
        //   })
    },


    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})